﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Web;
using System.Web.Mvc;
using PagedList;
using Sovigaz.DataProvider;
using Utilities;
using System.Data.SqlClient;

namespace Sovigaz.Controllers
{
    public class HomeController : Controller
    {
        private readonly SovigazDbContext _context = new SovigazDbContext();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Error()
        {
            ViewBag.Error = TempData["Error"];
            return View();
        }

        public PartialViewResult Navigation()
        {
            //if (String.IsNullOrWhiteSpace(MvcApplication.Navigation))
            {
                MvcApplication.SiteMaps = _context.Sitemaps.ToList();
                var sm = new StringBuilder();
                var context = new SovigazDbContext();
                var url = new UrlHelper(ControllerContext.RequestContext);

                var root = MvcApplication.SiteMaps.Where(p => String.IsNullOrWhiteSpace(p.ParentID)).OrderBy(p => p.Order).ToList();
                foreach (var r in root)
                {
                    sm.Append("<li>");
                    sm.Append("<a href=\"#\"><i class=\"fa " + ConvertUtility.StringParse(r.Icon) + " fa-fw\"></i>" + r.Title + "<span class=\"fa arrow\"></span></a>");

                    

                    var child1 = MvcApplication.SiteMaps.Where(p => p.ParentID == r.SitemapID).OrderBy(p => p.Order).ToList();

                    if (child1.Count > 0)
                    {
                        sm.Append("<ul class=\"nav nav-second-level\">");

                        foreach (var c1 in child1)
                        {
                            var permissions = context.Database.SqlQuery<sp_User_SelectPermission>(
                                    "sp_User_SelectPermission @UserID, @SitemapID"
                                    , new SqlParameter { ParameterName = "UserID", Value = Utility.UserID }
                                    , new SqlParameter { ParameterName = "SitemapID", Value = c1.SitemapID }).ToList();
                            bool permission = permissions.Exists(p => p.PermissionID == 1 && p.IsActived);

                            if (Utility.UserID==1 || permission)
                            {
                                sm.Append("<li>");

                                var child2 = MvcApplication.SiteMaps.Where(p => p.ParentID == c1.SitemapID).OrderBy(p => p.Order).ToList();
                                if (child2.Count > 0)
                                {
                                    sm.Append("<a href=\"#\">" + c1.Title + " <span class=\"fa arrow\"></span></a>");
                                    sm.Append("<ul class=\"nav nav-third-level\">");

                                    foreach (var c2 in child2)
                                    {
                                        permissions = context.Database.SqlQuery<sp_User_SelectPermission>(
                                            "sp_User_SelectPermission @UserID, @SitemapID"
                                            , new SqlParameter { ParameterName = "UserID", Value = Utility.UserID }
                                            , new SqlParameter { ParameterName = "SitemapID", Value = c2.SitemapID }).ToList();
                                        permission = permissions.Exists(p => p.PermissionID == 1 && p.IsActived);
                                        if (Utility.UserID == 1 || permission)
                                        {
                                            sm.Append("<li>");
                                            sm.Append("<a href=\"" + url.Action(c2.Action, c2.Controller) + "\">" + c2.Title + "</span></a>");
                                            sm.Append("</li>");
                                        }
                                    }

                                    sm.Append("</ul>");
                                }
                                else
                                    sm.Append("<a href=\"" + url.Action(c1.Action, c1.Controller) + "\">" + c1.Title + "</span></a>");

                                sm.Append("</li>");
                            }
                        }

                        sm.Append("</ul>");
                    }

                    sm.Append("</li>");
                }

                MvcApplication.Navigation = sm.ToString();
            }

            return PartialView("_Navigation");
        }
    }
}
